<?php

class System_ProblemsController extends Zend_Controller_Action
{

    public function init()
    {
        $this->view->baseUrl = $this->_request->getBaseUrl();
    }

    public function indexAction()
    {
        $this->view->title = 'Список задач';
        $problems = new System_Model_Problems();
        $this->view->problems = $problems->getProblems();
        
        $db = Zend_Db_Table::getDefaultAdapter();
        
        $select = $db->select()
                    ->from(array('a' => 'problems'))
                    ->join(array('b' => 'problem_theme'), 'a.theme_id = b.theme_id', array('theme' => 'title'))
                    ->order('a.problem_id');
        
        $paginator = new Zend_Paginator(new Zend_Paginator_Adapter_DbSelect($select));
        $paginator->setCurrentPageNumber($this->_getParam('page'));
        $paginator->setItemCountPerPage(5);
        $this->view->paginator = $paginator;
    }

    public function viewAction()
    {
        $id = $this->_request->getParam('id');
        $problem = new System_Model_Problems($id);
        $tests = new System_Model_Test();
        $test_example = $tests->getTests(array(
           'problem_id' => $id,
           'test_limit' => $problem->test_limit
        ));
        $this->view->example = $test_example;
        $this->view->problem = $problem;
        $form = new System_Form_Problem();
        
        if ($this->_request->isPost()) {
            $formData = $this->_request->getPost();
            if ($form->isValid($formData)) {
                    // success - do something with the uploaded file
                    
                    
                    $file = $form->file->getFileInfo();
                    $ext = pathinfo($file['file']['name'], PATHINFO_EXTENSION);
                    $newName = 'task.'.$ext;
                    
                    $fullFilePath = APPLICATION_PATH . DIRECTORY_SEPARATOR. 'modules'. DIRECTORY_SEPARATOR. 'system'. DIRECTORY_SEPARATOR. 'data'. DIRECTORY_SEPARATOR. 'uploads'. DIRECTORY_SEPARATOR. $newName;
                    
                    $form->file->addFilter('Rename', $fullFilePath); //добавляем фильтр «Rename» к элементу формы file, где передаём новое имя + полный путь к файлу на сервере
                    $form->file->receive(); //переносим файл в нашу папку из временной. Фильтр применяется автоматически
                    
                    switch ($ext) {
                        case 'cpp'  : $lang_prog_id = 1; break;
                        case 'c'    : $lang_prog_id = 1; break;
                        case 'cs'   : $lang_prog_id = 2; break;
                        case 'pas'  : $lang_prog_id = 3; break;
                        case 'java' : $lang_prog_id = 4; break;
                        default     : $lang_prog_id = 1; break;
                    }
                    
                    $source = new System_Model_Source();
                    $source->user_id = Zend_Auth::getInstance()->getIdentity()->user_id;
                    $source->problem_id = $id;
                    $source->lang_prog_id = $lang_prog_id;
                    $source->body = file_get_contents($fullFilePath);
                    $sid = $source->save();
                    unlink($fullFilePath);
                    
                    $status = new System_Model_Status();
                    
                    $status->date = time();
                    $status->user_id = Zend_Auth::getInstance()->getIdentity()->user_id;
                    $status->problem_id = $id;
                    $status->source_id = $sid;
                    $status->active = 1;
                    
//                    unlink("C:\tester\output.txt");
                    
                    
                    $db = Zend_Db_Table::getDefaultAdapter();
                    $select = $db->select()
                            ->from(array('status'), new Zend_Db_Expr('count(active) AS count'))
                            ->where('active = ?', 1)
                            ->group('active');
                    $result = $db->fetchAll($select);
                    if(!count($result)) {
                        // RUN TESTING PROCESS
                        // avrg 1 - source_id, 2 prog_lang_id, 3 - problem_id 
                        $exe = 'start /b /d "C:\tester\" C:\tester\mysql.exe ' . $sid . ' ' . $lang_prog_id . ' ' . $id;
                        pclose(popen($exe, "r"));
                    } else {
                        
                    }
                    $status->save();
                    $this->_redirect("system/status");
                    
                } else {
                    $form->populate($formData);
                }
            }
        $this->view->form = $form;
    }

    public function startAction()
    {
        
    }


}

